#include <ir/print.h>

namespace ir {
  Print::Print(std::ostream & _out) : out(_out) {}

  void Print::print(CompUnit & comp_unit) {
    for(InstGlobalAlloc * inst : comp_unit.inst_global_alloc_list) {
      std::string str;
      inst->print_get(str);
      this->out << str << "\n";
    }
    for(Func * func : comp_unit.func_list) {
      print(*func);
    }
  }

  void Print::print(Func & func) {
    this->out << "func " << func.name; 
    this->out << "(";
    for(unsigned long i = 0; i < func.arg_list.size(); ++i) {
      this->out << func.arg_list.at(i)->get_name();
      if(i != func.arg_list.size()-1) {
        this->out << ", ";
      }
    }
    this->out << "):\n";
    for(Block * block : func.block_list) {
      print(*block);
    }
  }

  void Print::print(Block & block) {
    this->out << block.get_name() << ":\n";
    for(Inst * inst : block.inst_list) {
      print(*inst);
    }
  }

  void Print::print(Inst & inst) {
    std::string str;
    inst.print_get(str);
    this->out << "\t" << str << "\n";
  }
}
